package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DepartmentMapper;
import com.ssy.bus.pojo.Department;
import com.ssy.bus.service.IDepartmentService;
import com.ssy.bus.vo.DepartmentVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @SuppressWarnings("unchecked")
    @Override
    public PageResult selectAllDepartment(DepartmentVo departmentVo) {
        Page<Department> page = new Page<>(departmentVo.getPage(), departmentVo.getLimit());
        IPage<Department> departments = this.departmentMapper.selectPage(page, new LambdaQueryWrapper<Department>()
                .like(StringUtils.hasText(departmentVo.getName()), Department::getName, departmentVo.getName())
                .eq(departmentVo.getState() != null, Department::getState, departmentVo.getState())
                .orderByDesc(Department::getLv));
        return new PageResult(departments.getTotal(), departments.getRecords());
    }

    @Override
    public Result addDepartment(Department department) {
        int i = this.departmentMapper.insert(department);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result deleteDepartmentById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.departmentMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Department queryDepartmentById(Integer id) {
        return this.departmentMapper.selectById(id);
    }

    @Override
    public Result bathDepartment(Integer[] ids) {
        if (ids.length == 0 || ids == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        int i = this.departmentMapper.deleteBatchIds(idList);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result LoadAllDepartment() {
        List<Department> departments = this.departmentMapper.selectList(new LambdaQueryWrapper<Department>()
                .eq(Department::getState, Constant.STATE));
        return new Result(departments);
    }

    @Override
    public Result updateDepartmentById(Department department) {
        if (department == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.departmentMapper.updateById(department);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public int clearRegister() {
        return this.departmentMapper.clearRegister();
    }
}
